﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ATW.MT.CommonClass;
using ATW.MT.BUS;
using System.Data;

namespace ATW.MT.RequireManage
{
    public partial class RequireManage : CommonPage
    {
        bus_T_DemandInformation info = new bus_T_DemandInformation();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {
            try
            {
                string title = txtTitle.Value.Trim();
                string type = ddlType.SelectedValue;
                string status = ddlStatus.SelectedValue;
                string Pstart = txtPubStart.Value.Trim();
                string Pend = txtPubEnd.Value.Trim();
                string Fstart = txtFinStart.Value.Trim();
                string Fend = txtFinEnd.Value.Trim();
                string name = txtName.Value.Trim();

                if (!common.filterChar(title) || !common.filterChar(name))
                {
                    Tips("查询条件含有非法字符！");
                    return;
                }
                DataTable dt = info.DemandPagerQuery(title, name, type, status, Pstart, Pend, Fstart, Fend, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize);
                if (dt != null && dt.Rows.Count > 0)
                {
                    AspNetPager1.RecordCount = Convert.ToInt32(dt.Rows[0]["totalPagesize"]);
                }
                else
                {
                    AspNetPager1.RecordCount = 0;
                }
                rpList.DataSource = dt;
                rpList.DataBind();
            }
            catch (Exception ex)
            {

            }

        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {//查询
            AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }
        protected void lbnDelete_Click(object sender, EventArgs e)
        {//删除
            LinkButton lbn = (LinkButton)sender;
            string tdiid = lbn.CommandArgument;
            try
            {
                Label lblnum = (Label)lbn.Parent.FindControl("lblNum");
                if (lblnum.Text != "0")
                {
                    Tips("已有投标，不能删除！");
                    return;
                }
                int result = info.DemandDel(tdiid);
                if (result > 0)
                {
                    Tips("删除成功");
                }
                else
                {
                    Tips("删除失败！");
                }
                BindData();
            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnRecover_Click(object sender, EventArgs e)
        {//恢复
            LinkButton lbn = (LinkButton)sender;
            string tdiid = lbn.CommandArgument;
            try
            {
                int result = info.DemandRecover(tdiid);
                if (result > 0)
                {
                    Tips("恢复成功");
                }
                else
                {
                    Tips("恢复失败！");
                }
                BindData();
            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnAudit_Click(object sender, EventArgs e)
        {//审核
            LinkButton lbn = (LinkButton)sender;
            string tdiid = lbn.CommandArgument;
            try
            {
                int result = info.DemandAudit(tdiid, "");
                if (result > 0)
                {
                    Tips("审核成功");
                }
                else
                {
                    Tips("审核失败");
                }
                BindData();
            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnBidDetail_Click(object sender, EventArgs e)
        {//查看竞标详情
            LinkButton lbn = (LinkButton)sender;
            string bid = lbn.CommandArgument;
            try
            {
                string dateEnd = info.GetDemandCloseDate(bid);
                DateTime _dateEnd = new DateTime();
                DateTime.TryParse(dateEnd, out _dateEnd);
                if (_dateEnd > DateTime.Now)
                {
                    Tips("未到截标时间，不能查看竞标详情！");
                    return;
                }
                string url = "../RequireManage/BidManage.aspx?bid=" + bid + "&r=" + new Random().Next(1000).ToString();
                OpenDialog("竞价详情", url, "height=550,width=850");
            }
            catch (Exception ex)
            {
 
            }
        }
        protected void AspNetPager1_pageChanged(object sender, EventArgs e)
        {//分页
            BindData();
        }
        private void Tips(string s)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }
        public string subStr(string s, int maxLen)
        {
            if (s.Length >= maxLen)
            {
                return s.Substring(0, maxLen - 3) + "...";
            }
            return s;
        }
    }
}